#include <net_util/http_util.h>
#include "ssicloud_oil_analysis_refuel.h"


ssicloud::OilAnalysisRefuel::OilAnalysisRefuel()
    : ListObject<OilAnalysisRefuelLNode>(LQ_BASE_URL "/oilAnalysis/refuel2"
                           , OBJECT_NAME_OilAnalysisRefuel
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);

    mUrl.append("vin", VIN_STRING);
}

ssicloud::OilAnalysisRefuel::~OilAnalysisRefuel()
{
    m_start.clear();
    m_end.clear();
}

bool ssicloud::OilAnalysisRefuel::requestAll(const char *start, const char *end, int pageNum, int pageSize)
{
    if (isLoading()) return true;
    HttpUtil::urlEncoding(start, m_start);
    HttpUtil::urlEncoding(end, m_end);

    m_pageNum = SFString::number(pageNum);
    m_pageSize = SFString::number(pageSize);

    mUrl.appendChange("start", m_start.c_str());
    mUrl.appendChange("end", m_end.c_str());

    mUrl.appendChange("pageNum", m_pageNum.c_str());
    mUrl.appendChange("pageSize", m_pageSize.c_str());

    return obtain();
}

void ssicloud::OilAnalysisRefuel::profile()
{
    ListTable<OilAnalysisRefuelLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------加油记录-------");

    for (; it != mNodes.end(); ++it)
    {
         GEN_Printf(LOG_DUMP, "ID:%s, title:%s, startAddress:%s, stopAddress:%s",
                    it->id.string(), it->title.c_str(), it->startAddress.c_str(), it->stopAddress.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::OilAnalysisRefuel::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    // GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        cJSON *data = cJSON_GetObjectItem(dataJson, "data");
        if (data)
        {
            for (cJSON *item = data->child; NULL != item; item = item->next )
            {
                OilAnalysisRefuelLNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, id, tmp.id);
                JSON_VALUETRING_SWAP_BYTESTRING(item, title, tmp.title);
                JSON_VALUETRING_SWAP_BYTESTRING(item, vin, tmp.vin);
                JSON_VALUETRING_SWAP_BYTESTRING(item, engineType, tmp.engineType);
                JSON_VALUETRING_SWAP_BYTESTRING(item, vehicleType, tmp.vehicleType);
                JSON_VALUETRING_SWAP_BYTESTRING(item, chassisNo, tmp.chassisNo);
                JSON_VALUETRING_SWAP_BYTESTRING(item, fuelType, tmp.fuelType);
                JSON_VALUETRING_SWAP_BYTESTRING(item, mainTankLevelDifference, tmp.mainTankLevelDifference);
                JSON_VALUETRING_SWAP_BYTESTRING(item, refuelingSign, tmp.refuelingSign);
                JSON_VALUETRING_SWAP_BYTESTRING(item, startTime, tmp.startTime);
                JSON_VALUETRING_SWAP_BYTESTRING(item, endTime, tmp.endTime);
                JSON_VALUETRING_SWAP_BYTESTRING(item, startLongitude, tmp.startLongitude);
                JSON_VALUETRING_SWAP_BYTESTRING(item, startLatitude, tmp.startLatitude);
                JSON_VALUETRING_SWAP_BYTESTRING(item, endLongitude, tmp.endLongitude);
                JSON_VALUETRING_SWAP_BYTESTRING(item, endLatitude, tmp.endLatitude);
                JSON_VALUETRING_SWAP_BYTESTRING(item, startAddress, tmp.startAddress);
                JSON_VALUETRING_SWAP_BYTESTRING(item, stopAddress, tmp.stopAddress);

                // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
                mNodes.push_back(tmp);
            }
        }

        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}
